package com.zch.house.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zch.house.entity.LeaseContract;
import com.zch.house.service.LeaseContractService;
import com.zch.house.service.impl.LeaseContractServiceImpl;
import com.zch.house.util.DateFormat;

/**
 * Servlet implementation class LeaseContractServlet
 */
@WebServlet("/LeaseContractServlet2")
public class LeaseContractServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	LeaseContractService lcs = new LeaseContractServiceImpl();   
	/**
     * @see HttpServlet#HttpServlet()
     */
    public LeaseContractServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("add".equals(method)){
			add(request,response);
		}else if("delete".equals(method)){
			delete(request,response);
		}else if("update".equals(method)){
			update(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("glcbi".equals(method)){
			getLeaseContractById(request,response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String lc_contract_number = request.getParameter("lcNumber");
		String flatIdStr = request.getParameter("flatId");
		int flat_id = Integer.parseInt(flatIdStr);
		String lesseeIdStr = request.getParameter("lesseeId");
		int tenant_id = Integer.parseInt(lesseeIdStr);
		String lcDateStr = request.getParameter("lcDate");
		Date lc_contract_date = DateFormat.to_Date2(lcDateStr);
		String lcStartDateStr = request.getParameter("lcStartDate");
		Date lc_contract_start_date = DateFormat.to_Date2(lcStartDateStr);
		String lcEndDateStr = request.getParameter("lcEndDate");
		Date lc_contract_end_date = DateFormat.to_Date2(lcEndDateStr);
		String totalRentStr = request.getParameter("totalRent");
		double lc_total_rent = Double.parseDouble(totalRentStr);
		String payWayStr = request.getParameter("payWay");
		int lc_pay_way = Integer.parseInt(payWayStr);
		String depositStr = request.getParameter("deposit");
		double lc_deposit = Double.parseDouble(depositStr);
		String periodOfPaymentStr = request.getParameter("periodOfPayment");
		int lc_period_payment = Integer.parseInt(periodOfPaymentStr);
		String lc_contract_signer = request.getParameter("lcSigner");
		String contractStateStr = request.getParameter("contractState");
		int lc_contract_state = Integer.parseInt(contractStateStr);
		//获得实体对象
		LeaseContract lc = new LeaseContract(lc_contract_number, flat_id, tenant_id,
				lc_contract_date, lc_contract_start_date, lc_contract_end_date, lc_total_rent, 
				lc_pay_way, lc_deposit, lc_period_payment, lc_contract_signer, lc_contract_state);
		boolean flag = lcs.addLeaseContract(lc);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='LeaseContractServlet2?method=select'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='LeaseContractServlet2?method=select'</script>");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		boolean flag = lcs.deleteLeaseContract(id);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='LeaseContractServlet2?method=select'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='LeaseContractServlet2?method=select'</script>");
		}

	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		String lc_contract_number = request.getParameter("lcNumber");
		String flatIdStr = request.getParameter("flatId");
		int flat_id = Integer.parseInt(flatIdStr);
		String lesseeIdStr = request.getParameter("lesseeId");
		int tenant_id = Integer.parseInt(lesseeIdStr);
		String lcDateStr = request.getParameter("lcDate");
		Date lc_contract_date = DateFormat.to_Date2(lcDateStr);
		String lcStartDateStr = request.getParameter("lcStartDate");
		Date lc_contract_start_date = DateFormat.to_Date2(lcStartDateStr);
		String lcEndDateStr = request.getParameter("lcEndDate");
		Date lc_contract_end_date = DateFormat.to_Date2(lcEndDateStr);
		String totalRentStr = request.getParameter("totalRent");
		double lc_total_rent = Double.parseDouble(totalRentStr);
		String payWayStr = request.getParameter("payWay");
		int lc_pay_way = Integer.parseInt(payWayStr);
		String depositStr = request.getParameter("deposit");
		double lc_deposit = Double.parseDouble(depositStr);
		String periodOfPaymentStr = request.getParameter("periodOfPayment");
		int lc_period_payment = Integer.parseInt(periodOfPaymentStr);
		String lc_contract_signer = request.getParameter("lcSigner");
		String contractStateStr = request.getParameter("contractState");
		int lc_contract_state = Integer.parseInt(contractStateStr);
		//获得实体对象
		LeaseContract lc = new LeaseContract(id, lc_contract_number, flat_id, 
				tenant_id, lc_contract_date, lc_contract_start_date, 
				lc_contract_end_date, lc_total_rent, lc_pay_way, lc_deposit, 
				lc_period_payment, lc_contract_signer, lc_contract_state);
		boolean flag = lcs.updateLeaseContract(lc);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将修改成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改成功！');window.location.href='LeaseContractServlet2?method=select'</script>");
		} else {
			// 修改失败,将修改失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改失败！');window.location.href='LeaseContractServlet2?method=select'</script>");
		}
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 详情
		String flag = request.getParameter("flag");
		String idStr = request.getParameter("id");
		if (flag == null || flag == "") {
			String number = request.getParameter("number");
			String startDateStr = request.getParameter("startDate");
			Date startDate = DateFormat.to_Date2(startDateStr);
			String endDateStr = request.getParameter("endDate");
			Date endDate = DateFormat.to_Date2(endDateStr);
			String stateStr = request.getParameter("state");
			if (stateStr == null || stateStr == "") {
				stateStr = "-1";
			}
			int state = Integer.parseInt(stateStr);
			String pageIndexStr = request.getParameter("pageIndex");
			if (pageIndexStr == null || pageIndexStr == "") {
				pageIndexStr = "1";
			}
			int pageIndex = Integer.parseInt(pageIndexStr);
			// 调用service层的方法
			List<LeaseContract> list = lcs.getAllLeaseContractByPage(number, startDate, endDate, state, pageIndex);
			// 获得总页码
			int totalPages = lcs.getTotalCount(number, startDate, endDate, state);
			// 将参数存入作用域
			request.setAttribute("number", number);
			request.setAttribute("startDate", startDate);
			request.setAttribute("endDate", endDate);
			request.setAttribute("state", state);
			request.setAttribute("pageIndex", pageIndex);
			request.setAttribute("list", list);
			request.setAttribute("totalPages", totalPages);
			request.getRequestDispatcher("view/leasecontract/list.jsp").forward(request, response);
		} else if ("detail".equals(flag)) {
			if (idStr != null && idStr != "") {
				int id = Integer.parseInt(idStr);
				LeaseContract lc = lcs.getLeaseContractById(id);
				request.setAttribute("lc", lc);
				request.getRequestDispatcher("view/leasecontract/detail.jsp").forward(request, response);
			}
		}
	}

	private void getLeaseContractById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		LeaseContract lc = lcs.getLeaseContractById(id);
		request.setAttribute("lc", lc);
		request.getRequestDispatcher("view/leasecontract/edit.jsp").forward(request, response);		
	}

}
